package com.sgb.kjwl.view.ui.activity.self.userInfo;

import android.content.Context;
import android.content.Intent;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.view.View;

import com.flyco.tablayout.listener.OnTabSelectListener;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityRegister3Binding;
import com.sgb.kjwl.view.event.CategoryEvent;
import com.swgk.core.event.EventConstant;
import com.sgb.kjwl.view.ui.fragment.self.EnterpriseRegistrationFragment;
import com.sgb.kjwl.view.ui.fragment.self.PersonalRegistrationFragment;
import com.swgk.core.recyclerview.adapter.FragPagerAdapter;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;

import static com.sgb.kjwl.view.ui.fragment.self.EnterpriseRegistrationFragment.RequestCodeE;
import static com.sgb.kjwl.view.ui.fragment.self.PersonalRegistrationFragment.RequestCodeP;

public class RegisterActivity3 extends AppActivity {
    private ActivityRegister3Binding binding;

    private FragPagerAdapter pagerAdapter;
    private ArrayList<Fragment> mFragments;
    String[] titles = {"个人注册", "企业注册"};
    private boolean again;
    EnterpriseRegistrationFragment enterpriseRegistrationFragment;
    PersonalRegistrationFragment personalRegistrationFragment;


    @Override
    protected boolean statusBarDark() {
        return true;
    }


    public static void start(Context context) {
        Intent intent = new Intent(context, RegisterActivity3.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(intent);
    }

    public static void start(Context context, boolean isActivity) {
        Intent intent = new Intent(context, RegisterActivity3.class);
        intent.putExtra("isActivity", isActivity);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(intent);
    }

    public static void start(Context context, boolean isActivity, boolean again,String companyId) {
        Intent intent = new Intent(context, RegisterActivity3.class);
        intent.putExtra("isActivity", isActivity);
        intent.putExtra("again", again);
        intent.putExtra("companyId", companyId);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(intent);
    }


    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(RegisterActivity3.this, R.layout.activity_register3);
        binding.include.titleView.setText("用户注册");

    }

    @Override
    protected void init() {
        mFragments = new ArrayList<>();
        boolean isActivity = getIntent().getBooleanExtra("isActivity", false);
        again = getIntent().getBooleanExtra("again", false);
        String companyId = getIntent().getStringExtra("companyId");
        if (isActivity) {
            enterpriseRegistrationFragment = EnterpriseRegistrationFragment.newInstance("1", again,companyId);
        } else {
            enterpriseRegistrationFragment = EnterpriseRegistrationFragment.newInstance("0", again,companyId);
        }
        personalRegistrationFragment = PersonalRegistrationFragment.newInstance("", "");
        mFragments.add(personalRegistrationFragment);
        mFragments.add(enterpriseRegistrationFragment);
        pagerAdapter = new FragPagerAdapter(getSupportFragmentManager(), mFragments);
        binding.include.ivBack.setOnClickListener((View) -> {
            finish();
        });
        binding.viewPager.setAdapter(pagerAdapter);
        binding.viewPager.setOffscreenPageLimit(titles.length - 1);
        binding.tl4.setTabData(titles);
        binding.tl4.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                binding.viewPager.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {
            }
        });
        binding.viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                binding.tl4.setCurrentTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        binding.include.ivBack.setOnClickListener((View) -> {
            if (again)
                EventBus.getDefault().post(CategoryEvent.make(EventConstant.EVENT_RE_AUTH_COM, null));
            finish();
        });

        if (isActivity) {
            binding.viewPager.setNoScroll(true);
            binding.viewPager.setCurrentItem(1);
            binding.include.titleView.setText("企业认证");
            binding.tl4.setVisibility(View.GONE);
        }
    }


    public void entSuccess() {
        binding.include.titleView.setText("企业认证");
        binding.tl4.setVisibility(View.GONE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case RequestCodeE:
                    enterpriseRegistrationFragment.onActivityResult(requestCode, resultCode, data);
                    break;
                case RequestCodeP:
                    personalRegistrationFragment.onActivityResult(requestCode, resultCode, data);
                    break;
                default:
                    break;
            }
        }
    }
}
